package org.example.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 接收消息处理类
 *
 * @author Brant
 * @since 2025-04-21
 */
@Service
@Slf4j
public class ReceiverMessageHandler implements MessageHandler {
    /**
     * 接收消息处理方法
     *
     * @param message 消息载体
     * @throws MessagingException 接收消息处理异常
     */
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        Object payload = message.getPayload();
        MessageHeaders headers = message.getHeaders();
        String receivedTopic = Objects.requireNonNull(headers.get("mqtt_receivedTopic")).toString();
        String receivedQos = Objects.requireNonNull(headers.get("mqtt_receivedQos")).toString();
        String timestamp = Objects.requireNonNull(headers.get("timestamp")).toString();
        log.info("MQTT payload= {} \n receivedTopic = {} \n receivedQos = {} \n timestamp = {}"
            , payload, receivedTopic, receivedQos, timestamp);
    }
}